<template>
  <base-dialog title="添加用户" v-bind="$attrs" v-on="$listeners" size="tiny" @submit="addUserRole">
        <el-form
                label-width="100px"
                class="demo-ruleForm"
                :rules="rulesUser"
                ref="addUserForm"
                :model="roleUser"
        >
            <el-form-item label="用户" prop="user_id">
                <el-select
                        v-model="roleUser.user_id"
                        filterable
                        remote
                        reserve-keyword
                        placeholder="请输入关键词"
                        :remote-method="getSimpleUser"
                        @change="changUser"
                >
                    <el-option
                            v-for="item in simpleUserList"
                            :key="item.id"
                            :label="item.alias"
                            :value="item.id"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="部门" prop="dept_id" v-if="currUser.user_dept_info_list">
                <el-radio-group v-model="roleUser.dept_id">
                    <el-radio :label="dept.id" v-for="(dept,index) in currUser.user_dept_info_list" :key="index">{{dept.name}}</el-radio>
                </el-radio-group>
            </el-form-item>
        </el-form>
  </base-dialog>
</template>

<script>
    import role from "@/api/systemManagement/role/index";
    import BaseDialog from '@/components/Common/BaseDialog'

    export default {
      components: { BaseDialog },
      props: {
            id: {
                type: Number,
                default: 0
            },
        },
        data() {
            return {
                roleUser: {user_id: "",dept_id:undefined},
                simpleUserList: [],
                rulesUser: {
                    user_id: [
                        {
                            required: true,
                            message: "请输入用户",
                            trigger: "blur"
                        }
                    ]
                },
                currUser:{}
            };
        },
        created() {
        },
        methods: {
            changUser(user){
                this.roleUser.dept_id = undefined
                this.simpleUserList.map(item=>{
                    if(item.id==user){
                        this.currUser = item
                        this.roleUser.dept_id = item.user_dept_info_list[0].id
                    }
                })
            },
            handleClose() {
                this.$emit("update:visible", false);
            },
            getSimpleUser(val) {
                role.getSimpleUser(val).then(res => {
                        this.simpleUserList = res.data.value;
                });
            },
            addUserRole() {
                this.$refs.addUserForm.validate(valid => {
                    if (valid) {
                        role
                            .addRoleUser(this.id, this.roleUser)
                            .then(res => {
                                this.msgSuccess("添加成功!");
                                this.$emit("reloadList");
                                this.handleClose();
                            });
                    } else {
                        return false;
                    }
                });
            }
        }
    };
</script>
